package com.ruoyi.internship.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.internship.mapper.BolePostionApplicationMapper;
import com.ruoyi.internship.domain.BolePostionApplication;
import com.ruoyi.internship.service.IBolePostionApplicationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 职位申请记录Service业务层处理
 * 
 * @author Wujiale
 * @date 2025-03-29
 */
@Service
public class BolePostionApplicationServiceImpl implements IBolePostionApplicationService 
{
    @Autowired
    private BolePostionApplicationMapper bolePostionApplicationMapper;

    @Autowired
     private ISysUserService sysUserService;
    @Override
    public int updateScore(BolePostionApplication bolePostionApplication,Integer score) {
        return sysUserService.updateScore(bolePostionApplication.getUserId(),score);
    }

    //查看自己有无申请过
    @Override
    public BolePostionApplication selectBolePostionApplicationByUserIdAndPositionId(BolePostionApplication bolePostionApplication) {
        return bolePostionApplicationMapper.selectBolePostionApplicationByUserIdAndPositionId(bolePostionApplication);
    }

    /**
     * 查询职位申请记录
     * 
     * @param applicationId 职位申请记录主键
     * @return 职位申请记录
     */
    @Override
    public BolePostionApplication selectBolePostionApplicationByApplicationId(String applicationId)
    {
        return bolePostionApplicationMapper.selectBolePostionApplicationByApplicationId(applicationId);
    }

    /**
     * 查询职位申请记录列表
     * 
     * @param bolePostionApplication 职位申请记录
     * @return 职位申请记录
     */
    @Override
    public List<BolePostionApplication> selectBolePostionApplicationList(BolePostionApplication bolePostionApplication)
    {
        return bolePostionApplicationMapper.selectBolePostionApplicationList(bolePostionApplication);
    }

    /**
     * 新增职位申请记录
     * 
     * @param bolePostionApplication 职位申请记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBolePostionApplication(BolePostionApplication bolePostionApplication,Integer score)
    {
        Long userId = SecurityUtils.getUserId();
        if (userId == 1) {
            return bolePostionApplicationMapper.insertBolePostionApplication(bolePostionApplication);
        }else {
//            不是管理员 扣积分  把id转成int
            int userId1 = Integer.parseInt(String.valueOf(userId));
            if (userId !=  userId1){
                //冒领他人
                return 0;
            }else{
                //更新积分
                updateScore(bolePostionApplication,score);
                return bolePostionApplicationMapper.insertBolePostionApplication(bolePostionApplication);
            }
        }

//        return bolePostionApplicationMapper.insertBolePostionApplication(bolePostionApplication);
    }

    /**
     * 修改职位申请记录
     * 
     * @param bolePostionApplication 职位申请记录
     * @return 结果
     */
    @Override
    public int updateBolePostionApplication(BolePostionApplication bolePostionApplication)
    {
        return bolePostionApplicationMapper.updateBolePostionApplication(bolePostionApplication);
    }

    /**
     * 批量删除职位申请记录
     * 
     * @param applicationIds 需要删除的职位申请记录主键
     * @return 结果
     */
    @Override
    public int deleteBolePostionApplicationByApplicationIds(String[] applicationIds)
    {
        return bolePostionApplicationMapper.deleteBolePostionApplicationByApplicationIds(applicationIds);
    }

    /**
     * 删除职位申请记录信息
     * 
     * @param applicationId 职位申请记录主键
     * @return 结果
     */
    @Override
    public int deleteBolePostionApplicationByApplicationId(String applicationId)
    {
        return bolePostionApplicationMapper.deleteBolePostionApplicationByApplicationId(applicationId);
    }
}
